package com.qc.wintrax.activity;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.qc.wintrax.BaseActivity;
import com.qc.wintrax.R;
import com.qc.wintrax.WTApplication;
import com.qc.wintrax.utils.PatinImageUtil;

import java.io.File;
import java.io.FileNotFoundException;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.qc.wintrax.R.id.imageView;

public class CamerActivity extends BaseActivity {


    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.camer_img)
    ImageView camerImg;
    @Bind(R.id.paint_img)
    ImageView paintImg;
    @Bind(R.id.saves_view)
    ImageView savesView;
    @Bind(R.id.backout_view)
    ImageView backoutView;
    @Bind(R.id.title_layout)
    RelativeLayout titleLayout;
    @Bind(R.id.imageView1)
    ImageView imageView1;
    @Bind(R.id.imageView2)
    ImageView imageView2;

    private String localTempImgDir = "wintrax";
    private String localTempImgFileName = ".qc.jpg";
    private Uri uri;
    private File f;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camer);
        ButterKnife.bind(this);
        backIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                WTApplication.getInstance().finishActivity();
                overridePendingTransition(R.anim.bottom_in, R.anim.bottom_out);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 1:
                    f = new File(Environment.getExternalStorageDirectory() + "/" + localTempImgDir + "/" + localTempImgFileName);
                    try {
                        uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(),
                                f.getAbsolutePath(), null, null));
                        //uri就是拍摄获得的原始图片的uri，剩下的你想干神马坏事请便……
                        PatinImageUtil.patinImg(context, uri, imageView1);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onConfigurationChanged(Configuration config) {
        super.onConfigurationChanged(config);
    }

    @OnClick({R.id.back_iv, R.id.camer_img, R.id.paint_img, R.id.saves_view, R.id.backout_view})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                WTApplication.getInstance().finishActivity();
                break;
            case R.id.camer_img:
                //先验证手机是否有sdcard
                String status = Environment.getExternalStorageState();
                if (status.equals(Environment.MEDIA_MOUNTED)) {
                    try {
                        File dir = new File(Environment.getExternalStorageDirectory() + "/" + localTempImgDir);
                        if (!dir.exists()) dir.mkdirs();
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        File f = new File(dir, localTempImgFileName);//localTempImgDir和localTempImageFileName是自己定义的名字
                        Uri u = Uri.fromFile(f);
                        intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
                        startActivityForResult(intent, 1);
                    } catch (ActivityNotFoundException e) {
                        Toast.makeText(CamerActivity.this, "没有找到储存目录", Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(CamerActivity.this, "没有储存卡", Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.paint_img:
                imageView1.setVisibility(View.GONE);
                imageView2.setVisibility(View.VISIBLE);
                PatinImageUtil.setImg(context, imageView2);
                break;
            case R.id.saves_view:

                break;
            case R.id.backout_view:
                PatinImageUtil.patinImg(context, uri, imageView1);
                break;
        }
    }
}
